package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderListService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author pengyehua
 * @date 2020/12/20 23:26
 */


@Service(interfaceClass = OrderListService.class)
@Component
public class OrderListServiceImpl implements OrderListService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter converter;

    @Override
    public OrderListResponse queryOrderDetailByUserId(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();
        //判空
        request.requestCheck();
        //分页
        PageHelper.startPage(request.getPage(), request.getSize());
        //查询order表
        Example example = new Example(Order.class);
        //排序
        example.setOrderByClause("create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", request.getUserId());
        List<Order> orders = orderMapper.selectByExample(example);
        List<OrderDetailInfo> orderDetailInfos = converter.order2details(orders);

        for (OrderDetailInfo orderDetailInfo : orderDetailInfos) {
            //根据orderId查询orderItem表
            Example example1 = new Example(OrderItem.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("orderId", orderDetailInfo.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);
            //根据orderId查询orderShipping表
            Example example2 = new Example(OrderShipping.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andEqualTo("orderId", orderDetailInfo.getOrderId());
            List<OrderShipping> orderShippings = orderShippingMapper.selectByExample(example2);
            //bean的转换
            List<OrderItemDto> orderItemDtos = converter.item2dto(orderItems);
            OrderShippingDto orderShippingDto = converter.shipping2dto(orderShippings.get(0));
            //封装orderItemDt和ordershippingDto
            orderDetailInfo.setOrderItemDto(orderItemDtos);
            orderDetailInfo.setOrderShippingDto(orderShippingDto);
        }
        PageInfo<OrderDetailInfo> pageInfo = new PageInfo<>(orderDetailInfos);
        long total = pageInfo.getTotal();
        response.setCode(OrderRetCode.SUCCESS.getCode());
        response.setMsg(OrderRetCode.SUCCESS.getMessage());
        response.setData(orderDetailInfos);
        response.setTotal(total);
        return response;

    }
}
